package com.plm.k8s.myk8sapi.cmd.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.plm.k8s.myk8sapi.cmd.AbstractSecretCmLocalCmd;
import com.plm.k8s.myk8sapi.cmd.ISecretCmd;
import com.plm.k8s.myk8sapi.pojo.entity.secret.ApiSecretEntity;
import com.plm.k8s.myk8sapi.pojo.entity.secret.SecretEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author 潘磊明
 * 版本号：1.0
 * 创建日期：2021/8/4 14:02
 */
@Service
public class SecretCmdImpl extends AbstractSecretCmLocalCmd implements ISecretCmd {

    @Override
    public List<SecretEntity> listSecret(boolean isAll, String namespace, String nameFilter,
            Map<String, String> labelFilter) throws Exception {
        String cmd = generateListSecretCmd(isAll, namespace, nameFilter, labelFilter);
        ApiSecretEntity apiSecretEntity = execForYamlObject(cmd, ApiSecretEntity.class);
        if (apiSecretEntity == null) return Collections.emptyList();
        return apiSecretEntity.getItems();
    }

    @Override
    public boolean createSecret(String name, Map<String, String> keyValue, List<File> files, String namespace) throws Exception {
        if (!StringUtils.hasText(name)) throw new Exception("Secret的名称不能为空");
        if (CollectionUtil.isEmpty(keyValue) && CollectionUtil.isEmpty(files)) throw new Exception("Secret的键值对和文件列表不能全为空");
        String cmd = generateCreateSecretCmd(name, keyValue, files, namespace);
        String result = exec(cmd);
        if (!String.format("secret/%s created", name).equals(trimBr(result))) {
            throw new Exception(result);
        }
        return true;
    }

    @Override
    public boolean deleteSecret(String name, String namespace) throws Exception {
        if (!StringUtils.hasText(name)) throw new Exception("Secret的名称不能为空");
        String cmd = generateDeleteSecretCmd(name, namespace);
        String result = exec(cmd);
        if (!String.format("secret \"%s\" deleted", name).equals(trimBr(result))) {
            throw new Exception(result);
        }
        return true;
    }

    private String generateDeleteSecretCmd(String name, String namespace) {
        StringBuilder sb = new StringBuilder(generateKubeCmdBase());
        sb.append("delete secret ").append(name);
        if (StringUtils.hasText(namespace)) addNamespace(sb, namespace);
        return sb.toString();
    }

    private String generateCreateSecretCmd(String name, Map<String, String> keyValue, List<File> files, String namespace) {
        StringBuilder sb = new StringBuilder(generateKubeCmdBase());
        sb.append("create secret generic ").append(name);
        if (CollectionUtil.isNotEmpty(keyValue)) addKeyValue(sb, keyValue);
        if (CollectionUtil.isNotEmpty(files)) addFile(sb, files);
        if (StringUtils.hasText(namespace)) addNamespace(sb, namespace);
        return sb.toString();
    }

    private String generateListSecretCmd(boolean isAll, String namespace, String nameFilter,
            Map<String, String> labelFilter) {
        StringBuilder sb = new StringBuilder(generateKubeCmdBase());
        sb.append("get secret -o yaml");
        if (isAll) {
            sb.append(" -A");
        } else {
            if (StringUtils.hasText(namespace)) addNamespace(sb, namespace);
        }
        if (StringUtils.hasText(nameFilter)) addNameFilter(sb, nameFilter);
        if (CollectionUtil.isNotEmpty(labelFilter)) addLabelFilter(sb, labelFilter);
        return sb.toString();
    }
}
